package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//套餐管理
@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private DishService dishService;


    //套餐列表
    @GetMapping("/setmeal/page")
    public ResultInfo findByPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                 String name){//接收参数
        // 2.调用service查询
        Page<Setmeal> page = setmealService.findByPage(pageNum, pageSize, name);
        // 3.返回结果
        return ResultInfo.success(page);
    }


    //新增套餐时回显菜品
    @GetMapping("/dish/list")
    public ResultInfo findListBySetmeal(Long categoryId,String name) {// 1.接收参数
        // 2.调用service
        List<Dish> dishList = dishService.findListBySetmeal(categoryId,name);
        // 3.返回结果
        return ResultInfo.success(dishList);
    }

    //新增
    @PostMapping("/setmeal")
    public ResultInfo saveSetmeal(@RequestBody Setmeal setmeal){
        //调用service新增
        setmealService.saveSetmeal(setmeal);

        //返回结果
        return ResultInfo.success();
    }

    //删除套餐
    @DeleteMapping("/setmeal")
    public ResultInfo deleteSetmeal(@RequestParam List<Long> ids){
        //调用service删除
        setmealService.deleteSetmeal(ids);

        //成功返回
        return ResultInfo.success();
    }

    //修改时回显数据
    @GetMapping("/setmeal/{id}")
    public ResultInfo findByIdd(@PathVariable(value = "id") Long id){
        //调用serivce查询
        Setmeal setmeal = setmealService.findByIdd(id);

        return ResultInfo.success(setmeal);
    }

    //修改套餐
    @PutMapping("/setmeal")
    public ResultInfo updateSetmeal(@RequestBody Setmeal setmeal){
        //调用service
        setmealService.updateSetmeal(setmeal);

        //返回结果
        return ResultInfo.success();
    }

    //起售,停售
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo updateStatusSetmeal(@PathVariable(value = "status") Integer status,
                                          @RequestParam List<Long> ids){
        //调用service
        setmealService.updateStatusSetmeal(status,ids);

        return ResultInfo.success();
    }




}
